package com.sr.maven.servlet.module;

import com.sr.maven.entity.Memorials;
import com.sr.maven.service.MemorialsService;
import com.sr.maven.service.impl.MemorialsServiceImpl;
import com.sr.maven.servlet.base.ModelBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class WorkServlet extends ModelBaseServlet {

    private MemorialsService memorialsService = new MemorialsServiceImpl();

    protected void showMemorialsDigestList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //. 1.调用Service方法查询数据
        List<Memorials> list = memorialsService.getAllMemorialsDigest();
        req.setAttribute("memorialsList", list);

        //将查询到的数据存入请求域
        String templateName = "memorials-list";

        processTemplate(templateName, req, resp);
    }

    protected void memorialsDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1。获取请求参数读取 memorialsId
        String memorialsId = req.getParameter("memorialsId");

        //2.根据memorialsId查询 对象

        Memorials memorials = memorialsService.getMemorialsById(memorialsId);

        //更新为 已读
        if (memorials.getMemorialsStatus() == 0) {
            memorialsService.updateMemorialsStatus(memorialsId, 1);
        }
        //3.放入请求域
        req.setAttribute("memorials", memorials);
        //解析渲染页面
        String templateName = "memorials-detail";
        processTemplate(templateName, req, resp);

    }

    protected void feedback(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.获取回复的内容参数
        String content = req.getParameter("feedbackContent");
        String memorialsId = req.getParameter("memorialsId");

        //更新内容
        memorialsService.updateFeedback(memorialsId,content);

        //重定向到列表页
        resp.sendRedirect(req.getContextPath()+"/work?method=showMemorialsDigestList");
    }
}
